/*******************************************************************************
*   Copyright (C) Harbin Institute of Technology, Shenzhen.
*				  All Rights Reserved.
*   Department: Decoration
********************************************************************************
* File Name   : sensor.h
* Author      : Dayuan
* Version     : v0.01
* Date        : 2019/9/2
* Description : Decoration Rototics Manipulator task.
*******************************************************************************/
/*-History----------------------------------------------------------------------
* Version   Date      Name        Changes and comments
* v0.01     2019/9/2  Dayuan    initial version
*=============================================================================*/
/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef _SENSOR_H_
#define _SENSOR_H_

#ifdef __cplusplus
 extern "C" {
#endif
     
/**** Includes ****/
#include "main.h"

/**** Definition of constants ****/

 
/**** Definition of types ****/ 
typedef enum
{
	SBT_CMD_READ = 0, //read cmd below
	SBT_GET_TORQUE,
	SBT_GET_BAUT,
	SBT_CUSTOM_READ,
	
	SBT_CMD_WRITE, //write cmd below
	SBT_SET_ZERO,
	SBT_SET_BAUT,
	SBT_LOCK,
	SBT_UNLOCK,
	SBT_CUSTOM_WRITE,
	
	SBT_CMD_ALL
}SBT_CMD;

typedef enum
{
	BAUT_1200 = 0x00,
	BAUT_2400,
	BAUT_4800,
	BAUT_9600,
	BAUT_19200,
	BAUT_38400,
	BAUT_57600,
	BAUT_115200,
	BAUT_230400,
	BUAT_ALL
}SBT_BUAT;

typedef enum
{
	SBT_COM_WRITE = 0,
	SBT_COM_WAIT,
	SBT_COM_READ,
	SBT_COM_IDLE
}SBT_COM_STATE;

/**** Definition of macros ****/
#define SBT_485_ADDR  0x01

#define SBT_485_READ  0x03
#define SBT_485_WRITE 0x10

#define SBT_TORQUE_ADDR_H 0x00
#define SBT_TORQUE_ADDR_L 0x50

#define SBT_BAUT_ADDR_H		0x00
#define SBT_BAUT_ADDR_L		0x01

#define SBT_ZERO_ADDR_H		0x00
#define SBT_ZERO_ADDR_L		0x5E

#define SBT_LOCK_ADDR_H   0x00
#define SBT_LOCK_ADDR_L 	0x05

#define SBT_TORQUE_REGISTER_NUM 0x02
#define SBT_BAUT_REGISTER_NUM 	0x01
#define SBT_ZERO_REGISTER_NUM 	0x01
#define SBT_LOCK_REGISTER_NUM 	0x01

#define SBT_BAUT_REGISTER_BYTE  0x02
#define SBT_ZERO_REGISTER_BYTE  0x02
#define SBT_LOCK_REGISTER_BYTE  0x02
#define SBT_READ_BYTE						0x00

#define SBT_UNLOCK_CMD 0x5AA5
#define SBT_LOCK_CMD   0xA55A

#define SBT_485_READ_TX_LEN 8
#define SBT_485_READ_RX_LEN 9

#define SBT_485_WRITE_TX_LEN 11
#define SBT_485_WRITE_RX_LEN 8

#define SBT_485_ENABLE_TX() HAL_GPIO_WritePin(RS485_RE_GPIO_Port,RS485_RE_Pin,GPIO_PIN_SET);

#define SBT_485_ENABLE_RX() HAL_GPIO_WritePin(RS485_RE_GPIO_Port,RS485_RE_Pin,GPIO_PIN_RESET);


/**** Declaration of constants ****/


/**** Declaration of variables ****/


/**** Declaration of functions ****/
static void delay_sbt_sensor(void);
static int sbt_send_cf(SBT_CMD cmd, uint16_t data);
static void temp_rd_cf(void);
static void temp_wr_cf(void);
#ifdef __cplusplus
}
#endif

#endif
